<?php

require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function index()
    {
        if (Request::isPost()) {
            $info = _POST('info', array());
            if (empty($info['user_name'])) {
                $this->msg('user name is empty');
            }
            if (empty($info['password'])) {
                unset($info['password']);
                
                if (!empty($info['full_name'])) {
                    $check = _model('user')->read(array('full_name'=>$info['full_name']));
                    if ($check && $check['id'] != $this->info['id']) {
                        $this->msg('full name exists');
                    }
                }

                $check_name = _model('user')->read(array('user_name'=>$info['user_name']));
                if ($check_name && $check_name['id'] != $this->info['id']) {
                    $this->msg('user name exists');
                }
                
                _model('user')->update_info($this->info['id'], $info);
                $_SESSION['admin_user'] = array_merge($_SESSION['admin_user'], $info);
                //@todo check msg
                $this->msg('Save was successful!', 'admin/my_account.html');
            } else {
                
                if (!empty($info['full_name'])) {
                    $check = _model('user')->read(array('full_name'=>$info['full_name']));
                    if ($check && $check['id'] != $this->info['id']) {
                        $this->msg('full name exists');
                    }
                }
                
                $check_name = _model('user')->read(array('user_name'=>$info['user_name']));
                if ($check_name && $check_name['id'] != $this->info['id']) {
                    $this->msg('user name exists');
                }
                $info['password'] = md5($info['password']);
                _model('user')->update_info($this->info['id'], $info);
                $this->redirect('admin/logout.html');
            }
        } else {
            $info = _model('user')->read(array('id'=>$this->info['id']));
            $this->view->assign('info', $info);
            $this->view->display('admin/my_account.html');
        }
    }
}


?>